package com.pfzy.utildemo.activity.tts;

import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.pfzy.utildemo.R;
import com.pfzy.utildemo.activity.base.BaseActivity;
import com.pfzy.utildemo.databinding.ActivityTtsBinding;

import java.util.HashMap;
import java.util.Locale;

// https://gitee.com/jackning_admin/SpeechTest/tree/master
// https://gitee.com/wiser-wong/VoiceReport/tree/master
public class TTSActivity extends BaseActivity implements View.OnClickListener, TextToSpeech.OnInitListener {
    private ActivityTtsBinding mBind;
    private TextToSpeech textToSpeech;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mBind = ActivityTtsBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());
        initView();
    }

    private void initView() {
        mBind.actTtsBnt1.setOnClickListener(this);
        textToSpeech = new TextToSpeech(this, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
        textToSpeech.shutdown(); // 关闭，释放资源
    }

    @Override
    public void onInit(int status) {
        if (textToSpeech != null) {
            int isSupportChinese = textToSpeech.isLanguageAvailable(Locale.CHINESE);// 是否支持中文
            textToSpeech.getMaxSpeechInputLength();// 最大播报文本长度

            if (isSupportChinese == TextToSpeech.LANG_AVAILABLE) {
                int setLanRet = textToSpeech.setLanguage(Locale.CHINESE);//设置语言
                int setSpeechRateRet = textToSpeech.setSpeechRate(1.0f);//设置语速
                int setPitchRet = textToSpeech.setPitch(1.0f);//设置音量
                String defaultEngine = textToSpeech.getDefaultEngine();//默认引擎
                if (status == TextToSpeech.SUCCESS) {
                    //初始化TextToSpeech引擎成功，初始化成功后才可以play等
                }
            }
        } else {
            //初始化TextToSpeech引擎失败
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.act_tts_bnt1:
                startAuto("语音播报 今天天气晴朗，风和日丽");
                break;
            default:
                break;
        }
    }

    private void startAuto(String data) {
        long utteranceId = System.currentTimeMillis();
        HashMap ttsOptions = new HashMap<String, String>();
        ttsOptions.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
                String.valueOf(utteranceId));//utterance，这个参数随便写，用于监听播报完成的回调中
        ttsOptions.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, String.valueOf(1));//音量
        ttsOptions.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
                String.valueOf(AudioManager.STREAM_NOTIFICATION));//播放类型
        int ret = textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, ttsOptions);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}

